<template>
  <div class="hero-slide has-quick-links is-video-and-image" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <template v-for="item in modelValue?.data" :key="item.id">
      <Swiper v-if="item.type === 'Banners'" ref="swiperRef" :data="item.data"></Swiper>
      <QuickLinks v-if="item.type === 'Links' && item.data.length" :data="item.data" @handleClick="handleClick"></QuickLinks>
    </template>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue" @updateSwiper="updateSwiper"></Setting>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Setting from './setting.vue'
import Operate from '@/common/Operate/index.vue'
import QuickLinks from '@/common/QuickLinks/index.vue'
import Swiper from '@/common/Swiper/index.vue'

// 定义组件名称
defineOptions({
  name: 'HeroSlide'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'handleClick'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'HeroSlide',
    data: [
      {
        id: randomString(),
        type: 'Banners',
        data: [
          {
            id: randomString(),
            type: 'Image',
            title: '耀华国际教育学校浙江桐乡',
            imgSrc: 'https://osswebsite-test.cedim.cn/media-library/ywies-tx/images/high-school-test/home/different_1_optim.png',
            alt: '',
            link: 'https://www.baidu.com',
          },
          {
            id: randomString(),
            type: 'Video',
            title: '耀华国际教育学校浙江桐乡',
            imgSrc: 'https://osswebsite-test.cedim.cn/media-library/ywies-tx/images/high-school-test/home/different_1_optim.png',
            alt: '',
            link: '',
            videoSrc: 'https://mvideo.ycyw.com/2e1a9441vodgzp1253458132/a31402d51397757891052265568/SW8B19vOtzMA.mp4'
          },
        ]
      },
      {
        id: randomString(),
        type: 'Links',
        data: [
          {
            id: randomString(),
            text: '了解更多',
            url: 'https://www.baidu.com',
            isExternal: false
          }
        ]
      }
    ]
  }
  emit('update:modelValue', data)
} 

const handleClick = (url: string) => {
  emit('handleClick', url)
}

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

const swiperRef = ref<typeof Swiper>()
const updateSwiper = () => {
  swiperRef.value?.initSwiper()
}

handleInit()
</script>

<style lang="scss" scoped>
.hero-slide {
  position: relative;
  overflow: hidden;
}
</style>